#include "controler.h"
#include "kaleidoscope.h"
#include "cairoicon.h"
#include "cairodock.h"
#include <QGridLayout>
#include <QtGui>

Controler::Controler(){
  setWindowState(Qt::WindowFullScreen);
  _model=new Kaleidoscope();
  QGridLayout * layout=new QGridLayout(this);
  layout->addWidget(_model,1,1,20,20);
  QList<CairoIcon*> *list = createMenu();
  layout->addWidget(new CairoDock(*list),18,1,3,20);
  show();

}

QList<CairoIcon*> * Controler::createMenu(){
    QList<CairoIcon*> *list=new QList<CairoIcon*>;
    CairoIcon *item=new CairoIcon(":/images/zmienOsie.png","Zmien\nOsie");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(changeAxis()));
    list->push_front(item);

    item=new CairoIcon(":/images/zmienRozmiar.png","Zmien\nRozmiar");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(changeRadius()));
    list->push_front(item);

    item=new CairoIcon(":/images/zmienObrazek.png","Zmien\nObrazek");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(changeTexture()));
    list->push_front(item);

    item=new CairoIcon(":/images/obroc.png","Obroc\nKaleidoskopem");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(changeAngle()));
    list->push_front(item);

    item=new CairoIcon(":/images/animacja.png","Wl/Wyl\nAnimacje");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(changeAnimation()));
    list->push_front(item);

    item=new CairoIcon(":/images/zapisz.png","Zapisz");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(savePixmap()));
    list->push_front(item);

    item=new CairoIcon(":/images/exit.png","Quit");
    QObject::connect(item,SIGNAL(clicked()),this,SLOT(exit()));
    list->push_back(item);
    return list;
}

void Controler::changeAxis(){
    QDialog dlg(this);
    QHBoxLayout *lay=new QHBoxLayout(&dlg);
    QSlider *slider = new QSlider(Qt::Horizontal);
    QPushButton *ok=new QPushButton("OK");
    QLabel *label=new QLabel;

    slider->setRange(2,20);
    slider->setValue(_model->getAxis());
    label->setNum(_model->getAxis());

    QObject::connect(slider,SIGNAL(valueChanged(int)),_model,SLOT(setAxis(int)));
    QObject::connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)));
    QObject::connect(ok,SIGNAL(clicked()),&dlg,SLOT(close()));

    lay->addWidget(label);
    lay->addWidget(slider);
    lay->addWidget(ok);

    dlg.exec();
    _model->update();
}
void Controler::changeTexture(){
        QString fileName = QFileDialog::getOpenFileName(this);
        if (!fileName.isEmpty()){
            _model->setTexture(fileName);
            _model->timerEvent(NULL);
        }
}

void Controler::exit(){
    emit quit();
}

void Controler::changeAngle(){
    QDialog dlg(this);
    QHBoxLayout *lay=new QHBoxLayout(&dlg);
    QSlider *slider = new QSlider(Qt::Horizontal);
    QPushButton *ok=new QPushButton("OK");
    QLabel *label=new QLabel;

    slider->setRange(0,360);
    slider->setValue(_model->getAngle());
    label->setNum(_model->getAngle());

    QObject::connect(slider,SIGNAL(valueChanged(int)),_model,SLOT(setAngle(int)));
    QObject::connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)));
    QObject::connect(ok,SIGNAL(clicked()),&dlg,SLOT(close()));

    lay->addWidget(label);
    lay->addWidget(slider);
    lay->addWidget(ok);

    dlg.exec();
    _model->update();
}

void Controler::changeRadius(){
    QDialog dlg(this);
    QHBoxLayout *lay=new QHBoxLayout(&dlg);
    QSlider *slider = new QSlider(Qt::Horizontal);
    QPushButton *ok=new QPushButton("OK");
    QLabel *label=new QLabel;

    slider->setRange(5,this->size().width()/2);
    slider->setValue(_model->getRadius());
    label->setNum(_model->getRadius());

    QObject::connect(slider,SIGNAL(valueChanged(int)),_model,SLOT(setRadius(int)));
    QObject::connect(slider,SIGNAL(valueChanged(int)),label,SLOT(setNum(int)));
    QObject::connect(ok,SIGNAL(clicked()),&dlg,SLOT(close()));

    lay->addWidget(label);
    lay->addWidget(slider);
    lay->addWidget(ok);

    dlg.exec();
    _model->update();
}
void Controler::changeAnimation(){
    static bool wl=false;
    wl=!wl;
    _model->setAnimation(wl);
}
void Controler::savePixmap(){

    QString fileName = QFileDialog::getSaveFileName(this);
        if (!fileName.isEmpty()){
            QPixmap obrazek = QPixmap::grabWidget(_model);
            obrazek.save(fileName);
        }

}
